<?php
// +----------------------------------------------------------------------
// | LaySNS
// +----------------------------------------------------------------------
// | QQ交流群: 227552979
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org
// +----------------------------------------------------------------------
// | github开源项目：https://gitee.com/yunyang0726/LaySNS
// +----------------------------------------------------------------------

namespace app\index\controller;

class Api
{

    public function index(){
//        $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
//        print_r(array_rand($a,1));
//        $arr=[1,2,3,4,5];
//        dd(array_rand($arr,1));
//
        $list=[
            1=>10,
            2=>10,
            3=>10,
            4=>10,
            5=>10,
        ];
        $arr=[1,2,3,4,5];

        $x=10;
        $a=[];
        $yue=$x;
        while($yue<=$x) {
            if(in_array($yue,$arr)){
                array_push($a,$yue);
                break;
            }
            $key=array_rand($arr);
            $b=$arr[$key];
            array_push($a,$b);
            $yue-=$b;
        }
        dd($a);

    }

    public function emlogTag(){

        $list=Db::name('tag')->slect();
        $article=new Article();
        foreach ($list as $k=>$v){
            $gids=explode(',',$v['gid']);
            foreach ($gids as $kk=>$vv){
                if($vv!==''){
                    $art=$article->find($vv);
                    if($art){
                        $oldKeywords=$art->keywords;
                        if($oldKeywords){
                            $art->keywords=$oldKeywords.','.$v['tagname'];
                        }else{
                            $art->keywords=$v['tagname'];
                        }
                        $art->save();
                    }


                }
                unset($gids[$kk]);
            }
            unset($list[$k]);
        }

    }

    public function qrcode($str)
    {

        if (!$str) {
            echo '参数丢失';
            exit();
        }
        //增加安全判断
        if (isset($_SERVER['HTTP_REFERER'])) {
            $referer_url = parse_url($_SERVER['HTTP_REFERER']);
            if ($referer_url['host'] != $_SERVER['HTTP_HOST']) {
               dd('非法请求');
            }
        }
        try {
            \PHPQRCode\QRcode::png($str);
            exit();
        } catch (\Exception $e) {
            echo $e->getMessage();
            exit();
        }
    }


    //最近更新的文章列表


}